<template>
  <div class="achievement-container">
    <!-- 顶部导航栏 -->
    <div class="top-nav">
      <div class="nav-left">
        <el-button @click="goHome" circle size="large" class="back-btn">
          <el-icon><ArrowLeft /></el-icon>
        </el-button>
      </div>
      <div class="nav-center">
        <div class="title-wrapper">
          <span class="trophy-icon">🏆</span>
          <h2 class="page-title">成就系统</h2>
        </div>
      </div>
      <div class="nav-right">
        <!-- 占位，保持居中 -->
      </div>
    </div>

    <!-- 统计信息卡片 -->
    <div class="stats-card">
      <div class="stats-content">
        <div class="stats-label">已解锁：</div>
        <div class="stats-value">
          <span class="achieved-count">{{ achievedCount }}</span>
          <span class="divider">/</span>
          <span class="total-count">{{ totalCount }}</span>
        </div>
      </div>
    </div>

    <!-- 筛选按钮组 -->
    <div class="filter-bar">
      <el-radio-group v-model="filterType" @change="filterAchievements" class="filter-group">
        <el-radio-button label="all">全部成就</el-radio-button>
        <el-radio-button label="achieved">已解锁</el-radio-button>
        <el-radio-button label="locked">未解锁</el-radio-button>
      </el-radio-group>
    </div>

    <div v-loading="loading" class="achievement-list">
      <div v-if="filteredList.length === 0" class="empty-state">
        <el-empty :description="getEmptyDescription()" />
      </div>

      <div v-else class="achievement-grid">
        <div
          v-for="item in filteredList"
          :key="item.id"
          class="achievement-card"
          :class="{ 'achieved': item.achieved, 'locked': !item.achieved }"
        >
          <div class="card-badge">
            <div class="badge-icon" :class="'level-' + item.level">
              {{ item.achieved ? '✨' : '🔒' }}
            </div>
            <div v-if="item.achieved" class="shine-effect"></div>
          </div>

          <div class="card-content">
            <h3 class="achievement-name">{{ item.name }}</h3>
            <p class="achievement-desc">{{ item.description }}</p>

            <div class="progress-section">
              <el-progress
                :percentage="getProgressPercentage(item)"
                :color="getProgressColor(item)"
                :stroke-width="8"
              />
              <p class="progress-text">
                {{ item.progress }} / {{ item.conditionValue }}
              </p>
            </div>

            <div class="achievement-footer">
              <el-tag :type="getLevelType(item.level)" size="small">
                {{ getLevelName(item.level) }}
              </el-tag>
              <span v-if="item.achieved" class="achieved-time">
                {{ formatDate(item.achievedAt) }}
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowLeft } from '@element-plus/icons-vue'
import { getUserAchievements } from '@/api'

const router = useRouter()

const loading = ref(false)
const achievementList = ref([])
const filterType = ref('all')

// 已解锁数量
const achievedCount = computed(() => {
  return achievementList.value.filter(item => item.achieved).length
})

// 总数量
const totalCount = computed(() => {
  return achievementList.value.length
})

// 过滤后的列表
const filteredList = computed(() => {
  if (filterType.value === 'achieved') {
    return achievementList.value.filter(item => item.achieved)
  } else if (filterType.value === 'locked') {
    return achievementList.value.filter(item => !item.achieved)
  }
  return achievementList.value
})

// 加载成就列表
const loadAchievements = async () => {
  loading.value = true
  try {
    const res = await getUserAchievements()
    if (res.code === 200) {
      achievementList.value = res.data || []
    } else {
      ElMessage.error(res.msg || '加载成就列表失败')
    }
  } catch (error) {
    console.error('加载成就列表失败：', error)
    ElMessage.error('加载成就列表失败')
  } finally {
    loading.value = false
  }
}

// 过滤成就
const filterAchievements = () => {
  // 触发计算属性更新
}

// 获取进度百分比
const getProgressPercentage = (item) => {
  if (!item.conditionValue) return 0
  const percentage = (item.progress / item.conditionValue) * 100
  return Math.min(percentage, 100)
}

// 获取进度条颜色
const getProgressColor = (item) => {
  if (item.achieved) {
    return '#67c23a'
  }
  const percentage = getProgressPercentage(item)
  if (percentage >= 80) return '#e6a23c'
  if (percentage >= 50) return '#409eff'
  return '#909399'
}

// 获取等级类型
const getLevelType = (level) => {
  const types = {
    1: 'info',
    2: 'warning',
    3: 'danger'
  }
  return types[level] || 'info'
}

// 获取等级名称
const getLevelName = (level) => {
  const names = {
    1: '🥉 铜牌',
    2: '🥈 银牌',
    3: '🥇 金牌'
  }
  return names[level] || '铜牌'
}

// 格式化日期
const formatDate = (dateStr) => {
  if (!dateStr) return ''
  const date = new Date(dateStr)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  return `${year}-${month}-${day}`
}

// 获取空状态描述
const getEmptyDescription = () => {
  if (filterType.value === 'achieved') {
    return '还没有解锁任何成就，加油哦！'
  } else if (filterType.value === 'locked') {
    return '恭喜！所有成就都已解锁！'
  }
  return '暂无成就数据'
}

// 返回主页
const goHome = () => {
  router.push('/home')
}

onMounted(() => {
  loadAchievements()
})
</script>

<style scoped>
.achievement-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0;
  background: linear-gradient(180deg, #ffe5f1 0%, #fff5f7 100%);
  min-height: 100vh;
}

/* 顶部导航栏 */
.top-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%);
  box-shadow: 0 2px 12px rgba(255, 105, 180, 0.2);
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-left,
.nav-right {
  flex: 0 0 60px;
}

.nav-center {
  flex: 1;
  display: flex;
  justify-content: center;
}

.title-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
}

.trophy-icon {
  font-size: 32px;
  animation: bounce 2s infinite;
}

@keyframes bounce {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-5px);
  }
}

.page-title {
  margin: 0;
  font-size: 24px;
  font-weight: bold;
  color: white;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
  letter-spacing: 2px;
}

.back-btn {
  background: white;
  border: none;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.back-btn:hover {
  transform: scale(1.1);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* 统计信息卡片 */
.stats-card {
  margin: 20px;
  padding: 20px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 4px 16px rgba(255, 105, 180, 0.15);
  border: 2px solid #ffe5f1;
}

.stats-content {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 12px;
}

.stats-label {
  font-size: 18px;
  color: #666;
  font-weight: 500;
}

.stats-value {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.achieved-count {
  font-size: 32px;
  font-weight: bold;
  color: #ff69b4;
  text-shadow: 2px 2px 4px rgba(255, 105, 180, 0.2);
}

.divider {
  font-size: 24px;
  color: #ccc;
  margin: 0 4px;
}

.total-count {
  font-size: 24px;
  font-weight: 600;
  color: #999;
}

/* 筛选按钮组 */
.filter-bar {
  margin: 0 20px 20px;
  text-align: center;
}

.filter-group {
  display: inline-flex;
  border-radius: 25px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(255, 105, 180, 0.15);
}

.filter-group :deep(.el-radio-button__inner) {
  padding: 12px 24px;
  border: none;
  background: white;
  color: #666;
  font-weight: 500;
  transition: all 0.3s ease;
}

.filter-group :deep(.el-radio-button__original-radio:checked + .el-radio-button__inner) {
  background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%);
  color: white;
  box-shadow: 0 2px 8px rgba(255, 105, 180, 0.3);
}

/* 成就列表区域 */
.achievement-list {
  min-height: 400px;
  padding: 0 20px 20px;
}

.empty-state {
  padding: 60px 0;
  background: white;
  border-radius: 20px;
  margin: 0 20px;
}

.achievement-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 20px;
}

.achievement-card {
  background: white;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.achievement-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
}

.achievement-card.achieved {
  background: linear-gradient(135deg, #fff5f7 0%, #ffe5f1 100%);
  border: 2px solid #ff69b4;
}

.achievement-card.locked {
  opacity: 0.7;
}

.card-badge {
  position: relative;
  width: 80px;
  height: 80px;
  margin: 0 auto 16px;
}

.badge-icon {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  background: linear-gradient(135deg, #f5f5f5 0%, #e0e0e0 100%);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.badge-icon.level-1 {
  background: linear-gradient(135deg, #cd7f32 0%, #b87333 100%);
}

.badge-icon.level-2 {
  background: linear-gradient(135deg, #c0c0c0 0%, #a8a8a8 100%);
}

.badge-icon.level-3 {
  background: linear-gradient(135deg, #ffd700 0%, #ffed4e 100%);
}

.shine-effect {
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.6), transparent);
  animation: shine 2s infinite;
}

@keyframes shine {
  0% {
    left: -100%;
  }
  100% {
    left: 100%;
  }
}

.card-content {
  text-align: center;
}

.achievement-name {
  margin: 0 0 8px 0;
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

.achievement-desc {
  margin: 0 0 16px 0;
  font-size: 14px;
  color: #666;
  line-height: 1.5;
  min-height: 42px;
}

.progress-section {
  margin-bottom: 16px;
}

.progress-text {
  margin: 8px 0 0 0;
  font-size: 14px;
  color: #999;
}

.achievement-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 12px;
  border-top: 1px solid #ffe5f1;
}

.achieved-time {
  font-size: 12px;
  color: #999;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .achievement-container {
    padding: 0;
  }

  .top-nav {
    padding: 12px 16px;
  }

  .page-title {
    font-size: 20px;
  }

  .trophy-icon {
    font-size: 28px;
  }

  .stats-card {
    margin: 16px;
    padding: 16px;
  }

  .stats-label {
    font-size: 16px;
  }

  .achieved-count {
    font-size: 28px;
  }

  .total-count {
    font-size: 20px;
  }

  .filter-bar {
    margin: 0 16px 16px;
  }

  .filter-group :deep(.el-radio-button__inner) {
    padding: 10px 18px;
    font-size: 14px;
  }

  .achievement-list {
    padding: 0 16px 16px;
  }

  .achievement-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }

  .achievement-card {
    padding: 20px;
  }
}
</style>

